package prototype.ex2;

import java.io.*;

public class Level implements Serializable{

    private String name;

    private String description;

    private Monster[] monsters;

    private Rewards rewards;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Monster[] getMonsters() {
        return monsters;
    }

    public void setMonsters(Monster[] monsters) {
        this.monsters = monsters;
    }

    public Rewards getRewards() {
        return rewards;
    }

    public void setRewards(Rewards rewards) {
        this.rewards = rewards;
    }

    public Level deepClone() {
        // 此方法实现了对象的深度克隆，使用序列化和反序列化的方式来复制当前对象
    try {
            //将对象写入输入流
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            //从输入流中获取对象
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            Level clone = (Level) ois.readObject();
            //关闭流
            bos.close();
            oos.close();
            bis.close();
            ois.close();
            return clone;
        } catch (Exception e) {
            System.out.println("克隆失败");
            return null;
        }

    }
}
